<template>
	<div ref="tableContainer" class="page">
        <el-card class="!border-none mb-4" shadow="never">
			<el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true" label-width="130px">
                <el-form-item label="创建时间" class="piker">
					<daterange-picker v-model:startTime="queryParams.start" v-model:endTime="queryParams.end" type="daterange" value-format="YYYY-MM-DD" />
				</el-form-item>
                <el-form-item label="用户">
					<el-input
						class="w-[240px]"
						v-model="queryParams.key"
						clearable
					/>
				</el-form-item>
                <el-form-item class="w-80">
                    <el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="序号" type="index" min-width="80" :index="indexAdd" />
                <el-table-column label="来源身份" min-width="180" prop="from_userinfo.realname"/>
                <el-table-column label="接收人身份" min-width="180" prop="to_userinfo.realname"/>
                <el-table-column label="内容" min-width="180" prop="msg_content"/>
                <el-table-column label="时间" min-width="180" prop="update_time"/>
                <el-table-column label="操作" width="180" fixed="right">
                    <template #default="{ row }">
                        <router-link
                            v-perms="['user.message/msgDetail']"
                            :to="{ path: getRoutePath('user.message/msgDetail'), query: { from: row.from, from_type: row.from_type, to: row.to, to_type: row.to_type  } }"
                        >
                            <el-link v-perms="['user.message/msgDetail']" type="primary" :underline="false">详情</el-link>
                        </router-link>
                        <el-button v-if="row.from_userinfo.is_forbidden == 0" v-perms="['user.user/detail']" class="ml-[10px]" type="danger" link @click="onNo(row)">禁言</el-button>
                        <el-button v-else v-perms="['user.user/detail']" class="ml-[10px]" type="warning" link @click="onNo(row)">解禁</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <!-- 禁言时间选择 -->
        <el-dialog
            v-model="dialogVisible"
            title="禁言"
            width="400"
            :close-on-click-modal="false"
        >
            <el-form ref="formRefTime" class="mb-[-16px]" :model="formData" :inline="true">
                <el-form-item class="inp" label="禁用到日期">
                    <el-date-picker
                        class="w-[280px]"
                        v-model="formData.times"
                        type="date"
                        placeholder="请选择日期"
                        value-format="X"
                    />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="onRight">确认</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup name="groupChart">
	import { msgList } from '@/api/group';
    import { forbidden  } from '@/api/consumer'
	import { usePaging } from '@/hooks/usePaging';
    import { getRoutePath } from '@/router'
    import { onBeforeRouteLeave } from 'vue-router'
    import type { FormInstance } from 'element-plus'
	let queryParams:any = reactive({
        start: '',
        end: '',
        key: ''
	});

	// 切页序号变化
    const indexAdd = (index: number) => {
        const page = pager.page // 当前页码
        const pagesize = pager.size // 每页条数
        return index + 1 + (page - 1) * pagesize
    }
	const { pager, getLists, resetPage, resetParams } = usePaging({
		fetchFun: msgList,
		params: queryParams,
        size: 100
	})

	const tableContainer:any = ref(null);
    let scrollPosition:any = ref(0);
	const saveScrollPosition = () => {
      if (tableContainer.value) {
        scrollPosition.value = tableContainer.value.scrollTop;
        sessionStorage.setItem('scrollPosition', scrollPosition.value)
      }
    };
    // 监听滚动
    // let lastScrollTop = 0;
    // const handleScroll = () => {
    //   const scrollTop = tableContainer.value.scrollTop;
    //   console.log(`Scroll position: ${scrollTop}`);
 
    //   // 在这里添加你的滚动逻辑，比如平滑滚动检测、无限滚动加载等
    //   if (scrollTop > lastScrollTop) {
    //     // 向下滚动
    //   } else {
    //     // 向上滚动
    //   }
    //   lastScrollTop = scrollTop <= 0 ? 0 : scrollTop; // 对于移动设备上当 scrollTop 为 0 时仍然不是最顶端的情况做处理
    // };
    onMounted(async () => {
		getLists();
        // tableContainer.value.addEventListener('scroll', handleScroll);
        nextTick(()=>{
            // 检查是否有保存的滚动位置，并在组件挂载后恢复它
            const pos = Number(sessionStorage.getItem('scrollPosition'))
            console.log(pos)
            if (pos && tableContainer.value ) {
                // 加了延迟才会生效
                setTimeout(()=>{
                    tableContainer.value.scrollTop = pos;
                },300)
            }
        })
	});
	// 钩子来保存滚动位置，当用户离开当前路由时触发
    onBeforeRouteLeave((to: any, from: any, next: () => void) => {
		console.log('离开')
		saveScrollPosition();
		next();
    });
    const formData = reactive({
        times: ''
    })
    const id = ref('')
    const formRefTime = shallowRef<FormInstance>()
    const onNo = (row:any) => {
        id.value = row.from_userinfo.user_id;
        if (row.from_userinfo.forbidden_time) {
            formData.times = row.from_userinfo.forbidden_time;
        } else {
            formData.times = '';
        }
        formRefTime.value?.clearValidate();
        dialogVisible.value = true;
    }
    const dialogVisible = ref(false)
    const onRight = async () => {
        console.log(formData)
        console.log(id.value)
        // 选择日期 小于当前日期
        // 获取当前的13位时间戳
        var timestamp = new Date().getTime();

        // 将13位时间戳转换为10位数的时间戳
        var tenDigitTimestamp = Math.round(timestamp / 1000).toString();
        console.log(tenDigitTimestamp)

        await forbidden({
            id: id.value,
            times: formData.times > tenDigitTimestamp ? formData.times: 0
        })
        dialogVisible.value = false;
        getLists()
    }

</script>
<style lang="scss" scoped>
.page{
    height: 800px;
    overflow-y: auto;
}
/* 针对Webkit内核的浏览器 */
.page::-webkit-scrollbar {
  display: none; /* 隐藏滚动条 */
}
 
/* 针对Firefox */
.page {
  scrollbar-width: none; /* 隐藏滚动条 */
}
.content{
    width: 360px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
.link{
    margin: 0 10px;
}
</style>